﻿using System;
using System.Web;
using AutoProbe.ProbeWebService;

namespace AutoProbe
{
    public class MedianamikProbe : IHttpModule
    {
        private static readonly Object thisLock = new Object();

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            if (context.Application["PROBE"] == null)
                context.BeginRequest += context_BeginRequest;
        }

        private void context_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Application["PROBE"] == null)
            {
                lock (thisLock)
                {
                    if (HttpContext.Current.Application["PROBE"] == null)
                    {
                        var probe = new Probing();
                        probe.ProbeAsync(new ProbeInfos().GetHostingInfos());
                        HttpContext.Current.Application["PROBE"] = DateTime.Now.ToString();
                    }
                }
            }
        }
    }
}